/**     
* @Title: SimpleFilterChain.java   
* @Package me.springboot.netty.filer   
* @Description: TODO
* @author weiwei 
* @date 2017年8月25日 下午2:56:22   
* @version V1.0     
*/
package me.springboot.netty.filer;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import static com.google.common.base.Preconditions.checkNotNull;

/**   
 * @ClassName: SimpleFilterChain   
 * @Description: TODO 
 * @author weiwei
 * @date 2017年8月25日 下午2:56:22   
 *      
 */
public class SimpleFilterChain implements FilterChain {
	
    private final Iterator<Filter> filterIterator;
    
    private final Servlet servlet;

    public SimpleFilterChain(Servlet servlet, Iterable<Filter> filters) throws ServletException {
        this.filterIterator = checkNotNull(filters).iterator();
        this.servlet = checkNotNull(servlet);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        if (filterIterator.hasNext()) {
            Filter filter = filterIterator.next();
            filter.doFilter(request, response, this);
        } else {
            servlet.service(request, response);
        }
    }
}
